﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;

namespace WinFromBook
{
    public partial class FrmLoad : CCSkinMain
    {
        public FrmLoad()
        {
            InitializeComponent();
        }
        #region 查询管理员是否存在
        int limits = 0;
        public bool GetAdmin()
        {
            if (new BLL.AdminInfoBLL().GetAdminInfo().Count==0)
            {
                MessageBox.Show("请检查数据库链接","系统提示");
                return false;
            }
            foreach (var item in new BLL.AdminInfoBLL().GetAdminInfo())
            {
                if (skinTextAname.Text.Trim() == item.Aname)
                {
                    if (skinTextApassword.Text.Trim() == decode(item.Apassword))
                    {
                        limits = item.AdminlimitsID;
                        if (item.Remark != "该管理员已注销")
                        {
                            return true;
                        }
                        MessageBox.Show("请检查用户名", "系统提示");
                        return false;
                    }
                    MessageBox.Show("密码不正确", "系统提示");
                    return false;
                }
            }
            MessageBox.Show("请检查用户名", "系统提示");
            return false;
        }


        /// 
        /// <summary>
        ///解密方式
        /// </summary>
        /// <param name="pass">数据库密码</param>
        /// <returns>返回参数</returns>
        public string decode(string pass)
        {
            try
            {

                //获取数据库字节数组
                byte[] by = Convert.FromBase64String(pass);
                //转换为字符串类型
                string pwd = Encoding.UTF8.GetString(by);
                //转换为Byte数组
                byte[] pw = Convert.FromBase64String(pwd);
                //转换为字符串类型
                string pd = Encoding.UTF32.GetString(pw);
                return pd;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (GetAdmin())
            {
                FrmMain FM = new FrmMain();
                FM.AdminName = skinTextAname.Text;
                FM.limits = limits;
                FM.Show();
                this.Hide();
            }
        }
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，大写英文，小写英文，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == '.') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        private void Cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }
    }
}
